<template>
  <HModal :visible="visible" :width="800" :height="600" title="校验结果" @cancel="close">
    <div class="message-filter-wrapper">
      <div @click="() => selectedType = undefined"
        :class="['message-filter-selector', !selectedType && 'message-filter-selected' || undefined]">
        全部 ({{messages.length}})
      </div>

      <div @click="() => selectedType = 'force'"
        :class="['message-filter-selector', selectedType === 'force' && 'message-filter-selected' || undefined]">
        不通过 (<span style="color: #f56c6c">{{messages.filter(m => m.forceCheck).length}}</span>)
      </div>

      <div @click="() => selectedType = 'warning'"
        :class="['message-filter-selector', selectedType === 'warning' && 'message-filter-selected' || undefined]">
        警告 (<span style="color: #e8a849">{{messages.filter(m => !m.forceCheck).length}}</span>)
      </div>
    </div>
    <div class="message-divider"/>
    <div class="message-wrapper" v-for="message in searchMessage">
      <div class="message-title">
        <div class="message-title-icon"/>
        <div class="message-title-text">
          {{ message.message }}
        </div>
        <div :class="['message-check-type', message.forceCheck && 'message-check-error' || 'message-check-warning']">
          <span class="iconfont">&#xe643;</span>
          {{ message.forceCheck ? '不通过' : '警告' }}
        </div>
      </div>

      <div class="message-body" v-if="message.content">
        <div class="message-body-content">
          {{message.content}}

          <div class="sub-form-message" v-if="message.formName">
            {{message.formName}} 的第 {{message.rowIndex + 1}} 行
          </div>
        </div>
      </div>
    </div>

    <div slot="footer">
      <a-button shape="round" @click="close">
        <span class="iconfont anticon">&#xe619;</span>
        关闭
      </a-button>
      <a-button v-if="!messages.filter(m => m.forceCheck).length"
        @click="handleOK" type="primary" shape="round" style="margin-left: 10px">
        <span class="iconfont anticon">&#xe621;</span>
        继续
      </a-button>
    </div>
  </HModal>
</template>
<script>
import {createProps} from '@/utils';
import HModal from '@comp/hhd/HModal/HModal.vue';

export default {
  name: 'FormCheckMessageModal',
  components: {
    HModal
  },
  props: {
    messages: createProps(Array),
    callback: createProps(Function),
  },
  data() {
    return {
      visible: false,
      selectedType: undefined
    };
  },
  mounted() {
    this.selectedType = undefined
    this.$nextTick(this.open);
  },
  methods: {
    close() {
      this.visible = false;
      this.$emit('close');
    },
    handleOK() {
      this.close();
      this.callback && this.callback();
    },
    open() {
      this.visible = true;
    },
  },
  computed: {
    searchMessage() {
      let list = this.messages || [];
      if(!this.selectedType) return list;
      if(this.selectedType === 'force') return list.filter(m => m.forceCheck);
      if(this.selectedType === 'warning') return list.filter(m => !m.forceCheck);

    },
  }
};
</script>
<style>
.message-wrapper {
  //border: 1px solid #eaecf0;
  border-radius: 4px;
  margin: 5px 10px 10px 10px;
  box-shadow: 0 0 5px #00000047;
}

.message-title {
  display: flex;
  padding: 10px;
  align-items: center;
  background: #f5f5f5;
}

.message-title .message-check-type {

}

.message-title .message-title-text {
  font-weight: bolder;
  flex: 1;
}

.message-title .message-title-icon {
  width: 5px;
  height: 16px;
  margin-right: 10px;
  background: #2c8fed;
  border-radius: 10px;
}

.message-check-error {
  color: #f56c6c
}

.message-check-warning {
  color: #e8a849
}

.message-body {
  border-top: 1px solid #eaecf0;
}

.message-body-content {
  padding: 10px 10px 10px 25px;
}

.sub-form-message {
  font-weight: bolder;
  font-size: 12px;
  margin-top: 6px;
}

.message-filter-wrapper {
  padding: 0 0 5px 10px;
}

.message-divider {
  margin: 5px 10px 11px 10px;
  height: 1px;
  background: #eaedf1;
}

.message-filter-selector {
  width: 130px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  border: 1px solid #e2e2e2;
  cursor: pointer;
  padding: 3px 0;
  margin-right: 5px;
}

.message-filter-selected span {
  color: white !important;
}
.message-filter-selected {
  background: #1890ff;
  color: white !important;
  border: 1px solid #1890ff;
}
</style>